// service/serverDetail/serverDetail.js
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: true,
    skill_id: '',
    list:[],
    finish: false,
    loading: false,
    page: 1,
    date: '',
    minDate: new Date(2023, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    show: false,
    showPopup: false,
    percent: ''
  },
  skillTime(){
    util.skip('/service/serverTime/serverTime?skill_id='+this.data.skill_id+'&name='+this.data.info.name)
  },
  onChange({ detail }) {
    http.post('skill/updateskillinfo',{type: 'update', is_rest: Number(!detail), skill_id: this.data.skill_id}).then(res => {
      this.setData({
        checked: detail
      })
    })
  },
  changePercent(){
    this.setData({
      showPopup: true
    })
  },
  onClosePercent(){
    this.setData({
      showPopup: false
    })
  },
  setPercent(e){
    this.setData({
      percent: e.Detail.value
    })
  },
  agree(){
    http.post('skill/updateskillinfo',{
      type: 'update', 
      skill_id: this.data.skill_id,
      percent: this.data.percent
    }).then(res => {
      util.toast(res.msg)
      this.data.info.percent  = this.data.percent
      this.setData({
        info: this.data.info,
        showPopup: false
      })
    })
  },
  showTime(){
    this.setData({
      show: true
    })
  },
  onClose(){
    this.setData({
      show: false
    })
  },
  onConfirm(event) {
   
    const [start, end] = event.Detail;
    let starttime = new Date(start.toLocaleDateString()).getTime(); 
    let endtime = new Date(end.toLocaleDateString()).getTime()+24*3600*1000 -1000;
    this.setData({
      show: false,
      starttime: starttime/1000,
      endtime: endtime/1000,
      date: `${util.formatTime(starttime,'yyyy/mm/dd')} - ${util.formatTime(endtime,'yyyy/mm/dd')}`,
    });
    this.reload()
  },
  orderBtnTap(e){
    if(e.Detail.type == 1){
      http.get('order/shopaccept', {
        id: e.Detail.info.id
      }).then(res => {
        this.setData({
          active: 1
        })
        this.reload()
        util.toast('抢单成功，请派单')
      })
    }else if(e.Detail.type == 2){
      util.skip('/pages/allocation/allocation?id='+e.Detail.info.id)
    }else if(e.Detail.type == 3){
      wx.openLocation({
        latitude : Number(e.Detail.info.address.lat),
        longitude : Number(e.Detail.info.address.lng),
        name: e.Detail.info.address.area,
        scale: 18
      })
    }
  },
  orderTap(e){
    util.skip('/pages/orderDetail/orderDetail?order_id='+e.Detail.id)
  },
  reload(){
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more(){
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList(){
    if(!wx.getStorageSync('token')) return
    if(this.data.finish){
      return 
    }
    if(this.data.loading){
      return 
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
      id: this.data.skill_id
    }
    if (this.data.starttime) {
      data['starttime'] = this.data.starttime
    }
    if (this.data.endtime) {
      data['endtime'] = this.data.endtime
    }
    http.get('shop/getshopskillinfo', data).then(res => {
      if(res.data.orderList.length === 0){
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data.orderList)
      this.setData({
        list: arr,
        info: res.data,
        checked: res.data.is_rest === 0,
        percent: res.data.percent
      })
      
    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      skill_id: options.skill_id
    })
    let  currentDate = new Date(new Date().toLocaleDateString()).getTime(); // 获取当前日期
    let end = currentDate+24*3600*1000 -1000;
    let start = currentDate - 24*3600*1000*30
    this.setData({
      starttime: start/1000,
      endtime: end/1000,
      date: `${util.formatTime(start,'yyyy/mm/dd')} - ${util.formatTime(end,'yyyy/mm/dd')}`
    })
    this.reload()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})